package com.edu.controller.comment;

import com.edu.dto.ResultDto;
import com.edu.entity.CommentEntity;
import com.edu.service.CommentService;
import com.edu.service.impl.CommentServiceImpl;
import com.edu.utils.ReturnCode;
import com.edu.utils.ServletHelper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.LinkedList;

/**
 * com.edu.controller.comment
 * 2024/7/14 14:19
 *
 * @author Fancy
 * @version 1.0
 */
    @WebServlet(urlPatterns = {"/reviews", "/reviews/*"})
public class CommentController extends HttpServlet {
    CommentService commentService = new CommentServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        CommentEntity comment = ServletHelper.getRequestBodyAsObject(req, CommentEntity.class);

        int row = commentService.addComment(comment);

        if (row >= 1) {
            String success = ResultDto.success(null);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(success);
        } else {
            String fail = ResultDto.fail(ReturnCode.UNAUTHORIZED.getCode(), "发布评论失败");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        }

    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        String[] reqUrl = req.getPathInfo().split("/");
        int reviewId = Integer.parseInt(reqUrl[1]);
        String token = req.getHeader("token");

        int row = commentService.deleteReview(reviewId, token);

        if (row >= 1) {
            String success = ResultDto.success(null);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(success);
        } else if (row == 0) {
            String fail = ResultDto.fail(ReturnCode.UNAUTHORIZED.getCode(), "删除评论失败");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        } else {
            String fail = ResultDto.fail(ReturnCode.INTERNAL_SERVER_ERROR.getCode(), "内部错误");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        String[] reqUrl = req.getPathInfo().split("/");
        int bookId = Integer.parseInt(reqUrl[1]);

        //记录： 在删除用户的时候其它以用户ID为主键的表的记录也会删除
        LinkedList<CommentEntity> commentEntities = commentService.getReviewsByBookId(bookId);

        if (commentEntities != null) {
            String success = ResultDto.success(commentEntities);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(success);
        } else {
            String fail = ResultDto.fail(ReturnCode.UNAUTHORIZED.getCode(), "获取评论失败");
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getWriter().write(fail);
        }
    }
}